﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using LIM.forms.uc_SYS_QuanTriNguoiDung;

using LIM.controller;
using LIM.model;
using Telerik.WinControls.UI;
using System.Threading;

namespace LIM.forms
{
    public partial class radForm_SYS_QuanTriNguoiDung : BaseRadForm
    {
        public SYSController _SYSController = new SYSController();

        public delegate void GridViewDoubleClick();
        public event GridViewDoubleClick OnRowChange;

        public tbl_DanhMuc_ThongTinNhanVien SelectedEntity { set; get; }

        uc_SYS_QuanTriNguoiDung_ThongTinNhanVien uc_SYS_QuanTriNguoiDung_ThongTinNhanVien;
        uc_SYS_QuanTriNguoiDung_NhomNguoiDung uc_SYS_QuanTriNguoiDung_NhomNguoiDung; 

        public radForm_SYS_QuanTriNguoiDung()
        {
            InitializeComponent();
            
            uc_SYS_QuanTriNguoiDung_ThongTinNhanVien = new uc_SYS_QuanTriNguoiDung_ThongTinNhanVien();
            uc_SYS_QuanTriNguoiDung_ThongTinNhanVien.Dock = DockStyle.Fill;
            this.radPageViewPage_ThongTinNhanVien.Controls.Add(uc_SYS_QuanTriNguoiDung_ThongTinNhanVien);

            uc_SYS_QuanTriNguoiDung_NhomNguoiDung = new uc_SYS_QuanTriNguoiDung_NhomNguoiDung();
            uc_SYS_QuanTriNguoiDung_NhomNguoiDung.Dock = DockStyle.Fill;            
            this.radPageViewPage_NhomNguoiDung.Controls.Add(uc_SYS_QuanTriNguoiDung_NhomNguoiDung);

            // Khai báo các controls sẽ áp dụng phân quyền trong hệ thống
            //ControlPermissionList.Add(this.radMenuItem_Refresh);
            ControlPermissionList.Add(this.radMenuItem_TaoMoi);
            // Kết thúc khai báo

        }

        private void radForm_SYS_QuanTriNguoiDung_Load(object sender, EventArgs e)
        {
            radGridView_DanhSachNhanVien.DataSource = _SYSController.LayDanhSach_ThongTinNhanVien();
            radGridView_DanhSachNhanVien.ClearSelection();            
        }                
        
        private void radGridView_DanhSachNhanVien_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //if (IsClickedOnRowContent((RadGridView)sender, e.Location))
            //{
            //    SelectedEntity = (tbl_DanhMuc_ThongTinNhanVien)this.radGridView_DanhSachNhanVien.CurrentRow.DataBoundItem;
            //    if (OnRowChange != null)
            //        OnRowChange();
            //}
        }

        /// <summary>
        /// Detect click on rows
        /// </summary>
        /// <param name="radGridView"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        private bool IsClickedOnRowContent(RadGridView radGridView, Point location)
        {
            int excludeHeight = 0;
            if (radGridView.ShowFilteringRow)
                excludeHeight += radGridView.TableElement.FilterRowHeight;
            if (radGridView.ShowGroupPanel)
                excludeHeight += radGridView.TableElement.GroupHeaderHeight;
            if (radGridView.ShowColumnHeaders)
                excludeHeight += radGridView.TableElement.TableHeaderHeight;
            if (excludeHeight < location.Y)
            {                
                return true;
            }

            return false;
        }

        public void RefreshData()
        {
            _SYSController.Refresh_tbl_DanhMuc_ThongTinNhanVien();
            radForm_SYS_QuanTriNguoiDung_Load(new object(), new EventArgs()); 
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void radMenuItem_TaoMoi_Click(object sender, EventArgs e)
        {            
            radGridView_DanhSachNhanVien.ClearSelection();
            SelectedEntity = new tbl_DanhMuc_ThongTinNhanVien();
            if (OnRowChange != null)
                OnRowChange();
        }

        private void radGridView_DanhSachNhanVien_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            SelectedEntity = (tbl_DanhMuc_ThongTinNhanVien)this.radGridView_DanhSachNhanVien.CurrentRow.DataBoundItem;
            if (OnRowChange != null)
                OnRowChange();
        }

        
    }
}
